/**
 * 安全奖i订单 编辑页
 *
 * 作者 zhangwei
 * 日期 2024-09-02 11:28:06
 */
layui.use(['jquery','febs', 'form', 'validate','table','commonJS'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        tableIns,
        $view = $('#incentiveGoodsOrder-handOut'),

        validate = layui.validate;

    form.verify(validate);
    form.render();
    initFormValue();

    function initFormValue() {
       if(incentiveGoodsOrder){
           form.val("incentiveGoodsOrder-handOut-form", {
               "goodsOrderId": incentiveGoodsOrder.goodsOrderId,
           });
           $("#people").text(incentiveGoodsOrder.applyUserNames)
       }
    }

    form.on('submit(incentiveGoodsOrder-handOut-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });

    form.on('submit(incentiveGoodsOrder-cancel-form-submit)', function (data) {
        cancel(data.field)
        return false;
    });

    function getTenantId(){
        if(incentiveGoodsOrder){
            return incentiveGoodsOrder.tenantId;
        }else {
            return POPUP_DATA.tenantId;
        }
    }


    initTable(itemList);

    function initTable(data) {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'goodsTable',
            page:false,
            data:data,
            skin: '',
            cols: [[
                {type: 'numbers', title: '序号'},
                {field: 'goodsName', title: '奖品名称', minWidth: 120},
                {field: 'goodsNumber', title: '奖品数量', width: 120},
                {field: 'lockNumber', title: '锁定数量', width: 120},
            ]],
            defaultToolbar: []
        });
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload();
    });

    $view.find("#scanIcon").on('click',function () {
        alert(1)
        // TODO 扫描奖品二维码
    })

    function addOrUpdate(data) {
        febs.modal.confirm("保存提示！","是否兑现？",function (index){
            incentiveGoodsOrder.userList = userList;
            incentiveGoodsOrder.goodsList = itemList;
            incentiveGoodsOrder.code = data.code;
            if (data.goodsOrderId && data.goodsOrderId.length > 0) {
                febs.postArray(ctx + 'incentiveGoodsOrder/handOut', incentiveGoodsOrder, function () {
                    layer.closeAll();
                    febs.alert.success('发放成功');
                    $('#febs-incentiveGoodsOrder').find('#query').trigger('click');
                });
            } else {
                febs.postArray(ctx + 'incentiveGoodsOrder', data, function () {
                    layer.closeAll();
                    febs.alert.success('新增成功');
                    $('#febs-incentiveGoodsOrder').find('#query').trigger('click');
                });
            }
        })
    }

    function cancel(data) {
        febs.modal.confirm("保存提示！","是否取消兑现？",function (index){
            incentiveGoodsOrder.userList = userList;
            incentiveGoodsOrder.goodsList = itemList;
            incentiveGoodsOrder.code = data.code;
            if (data.goodsOrderId && data.goodsOrderId.length > 0) {
                febs.postArray(ctx + 'incentiveGoodsOrder/cancel', incentiveGoodsOrder, function () {
                    layer.closeAll();
                    febs.alert.success('取消兑现成功');
                    $('#febs-incentiveGoodsOrder').find('#query').trigger('click');
                });
            }
        })
    }
});